package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocal;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Autowired
    private StringRedisTemplate ridesTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 用户关注行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follw(FollowBehaviorDto dto) {

        //校验参数
        if (dto.getArticleId()==null ||dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取登录人用户id
        ApUser apUser = ApThreadLocal.getApUser();

        //未登录
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //有课身份不能进行以下操作
        if(apUser.getId()==0){
            return ResponseResult.okResult(405,"游客身份禁止，需进行登录");
        }
        //用户是否已经关注
        //定义关注的key
        String follow_key ="USER_FOLLOW_"+apUser.getId();
        //定义粉丝的key
        String fans_key="USER_FANS_"+dto.getAuthorId();
        //查询redis信息，key=USER_FOLLOW_+{userId}
        Boolean flg = ridesTemplate.opsForHash().hasKey(follow_key, dto.getAuthorId() + "");
        //已关注 operation ==0 已经关注
        if (flg&&dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"用户已关注");
        }
        //已经关注，取消关注操作 删除redis 和mongDB
        else if (flg&&dto.getOperation()==1){
            //删除redis
            ridesTemplate.opsForHash().delete(follow_key,dto.getAuthorId()+"");
            ridesTemplate.opsForHash().delete(fans_key,apUser.getId()+"");

            //删除mongo
            Query query =Query.query(Criteria.where("userId").is(apUser.getId()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query, ApFollowBehavior.class);
        }

        //用户没用关注，执行关注操作
        if (!flg&&dto.getOperation()==0){
            //新增redis
            ridesTemplate.opsForHash().put(follow_key,dto.getAuthorId()+"","1");
            ridesTemplate.opsForHash().put(fans_key,apUser.getId()+"","1");
            //新增mongo
            ApFollowBehavior apFollowBehavior =new ApFollowBehavior();
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setCreatedTime(new Date());
            apFollowBehavior.setUserId(apUser.getId().longValue());
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setId(ObjectId.get().toHexString());
            mongoTemplate.insert(apFollowBehavior);
        }

        return ResponseResult.okResult("关注用户成功");
    }
}
